﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Office.Interop.Excel;

namespace ObjectModel.Excel.Old
{
    /// <summary>
    /// Serializable base class for any sub classes extending an Excel range object. Assumes that
    /// the underlying range object will be available for the duration of this object's life.
    /// </summary>
    public class CachedRange : OfflineRange
    {
        private Range mRange; // Cached range
        
        [XmlIgnore]
        public virtual string Address { get { return Range.Address; } }

        public string LastAddress { get; set; } // To be serialized. Must be public.

        // Overrides base class to store and retrieve a live range object, getting
        // the range object from serializable info only once, immediately after deserialization.
        [XmlIgnore]
        public override Range Range
        {
            get { return mRange; }
            set
            {
                mRange = value;
                LastAddress = Address;
            }
        }

        public virtual dynamic Value
        {
            get { return Range.Value; }
            set { Range.Value = value; }
        }

        [XmlIgnore]
        public override Worksheet Worksheet { get { return Range.Worksheet; } }

        [XmlIgnore]
        public override Workbook Workbook { get { return Worksheet.Parent; } }

        // Parameterless constructor for serialization. Gets the cached range from serialized info.
        public CachedRange() { Range = base.Worksheet.Range[LastAddress]; }

        public CachedRange(Range range) { Range = range; }
    }
}
